package com.espeaker.sdk.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import org.json.JSONObject;

/**
 * Json工具类
 * @author jimmy
 */
public class JsonUtils {

    private static final String STR_NULL = "null";

	private static Map<String, SimpleDateFormat> formatMap = new HashMap<String, SimpleDateFormat>();

	public static Date getDate(JSONObject json, String key, String format) {
        String str = json.optString(key);
        if (str == null || "".equals(str) || STR_NULL.equals(str.toLowerCase())) {
        	return null;
        }
    	SimpleDateFormat sdf = formatMap.get(format);
        if (null == sdf) {
            sdf = new SimpleDateFormat(format, Locale.ENGLISH);
            sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
            formatMap.put(format, sdf);
        }
        try {
            synchronized(sdf){
                return sdf.parse(str);
            }
        } catch (ParseException pe) {
            throw new com.espeaker.sdk.ParseException("Unexpected format(" + str + ")");
        }
    }
	
	public static String getString(JSONObject json, String key) {
        String str = json.optString(key);
        if (null == str || STR_NULL.equals(str.toLowerCase())) {
            return null;
        }
	    return str;
	}
	
	public static int getInt(JSONObject json, String key, int defaultValue) {
        String str = json.optString(key);
        //System.out.println(key + ":" + str);
        if (null == str || "".equals(str) || STR_NULL.equals(str.toLowerCase())) {
            return defaultValue;
        }
        return Integer.parseInt(str);
    }
	public static JSONObject getJSONObject(JSONObject json, String key, JSONObject defaultValue) {
		JSONObject str = json.optJSONObject(key);
        //System.out.println(key + ":" + str);
        if (null == str) {
            return defaultValue;
        }
        return str;
    }  
	public static long getLong(JSONObject json, String key, long defaultValue) {
		String str = json.optString(key);
        if (null == str || "".equals(str) || STR_NULL.equals(str.toLowerCase())) {
            return defaultValue;
        }
        return Long.parseLong(str);
    }
	
	public static float getFloat(JSONObject json, String key, float defaultValue) {
		String str = json.optString(key);
        if (null == str || "".equals(str) || STR_NULL.equals(str.toLowerCase())) {
            return defaultValue;
        }
        return Float.parseFloat(str);
	}

    public static double getDouble(JSONObject json, String key, double defaultValue) {
        String str = json.optString(key);
        if (null == str || "".equals(str) || STR_NULL.equals(str.toLowerCase())) {
            return defaultValue;
        }
        return Double.parseDouble(str);
    }
	
	public static boolean getBoolean(JSONObject json, String key, boolean defaultValue) {
        String str = json.optString(key);
        if (null == str || "".equals(str) || STR_NULL.equals(str.toLowerCase())) {
            return defaultValue;
        }
        if ("0".equals(str)) {
        	return false;
        } else if ("1".equals(str)) {
        	return true;
        }
        return Boolean.valueOf(str);
    }

    public static String[] getStringArray(JSONObject json, String key, String regularExpression) {
        String str = json.optString(key);
        if (null == str || STR_NULL.equals(str.toLowerCase())) {
            return null;
        }

        return str.split(regularExpression);
    }
}
